/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.schedule;

import android.content.ContentValues;
import android.util.Log;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.GregorianCalendar;

/**
 *
 * @author peterholtegaard@gmail.com
 */
public class Day {

    private String date;//the date
    private String meetingTime;//start time of event
    private String timeOff;//end time of event
    private String refLink;//link to event details
    private String title;
    private int month;
    private int year;
    private int allday;

    private Day() {
    }

    //specific constructors for "creating" day objects from different calendars
    public static Day dayFromTTVString(String dayString, int month) {
        Log.e("link", "bol" + dayString);
        Day day = new Day();

        return day;
    }

    public static Day dayFromgCalEntry() {
        return null;
    }

    public static Day getSpecifiedDay(String parseable) {
        Log.e("parseabel: ", parseable);
        Day d = new Day();
        StringCharacterIterator it = new StringCharacterIterator(parseable);
        StringBuilder result = new StringBuilder();
        boolean relevant = false;

        while (it.next() != StringCharacterIterator.DONE) {
            //String check = String.valueOf(it.current());
            char current = it.current();
            if (relevant) {
                result.append(current);

            }
            //if (it.current() == '>')return null;
            if (current == '<') {
                relevant = false;

            } else if (current == '>') {
                relevant = true;

            }
        }
        String[] step2 = result.toString().split("<");
        StringBuilder two = new StringBuilder();
        int z = 1;
        for (int i = 0; i < step2.length; i++) {

            String s = step2[i].trim();
            try {
                if (s.length() != 0) {

                    two.append(s).append(" <> ");
                    if (z == 1) {
                        d.year = 2000 + Integer.parseInt(s.substring(4, 5));
                        d.month = Integer.parseInt(s.substring(7, 8));
                        d.date = s.substring(9, 11);
                    } else if (z == 2) {
                        String[] hours = s.split("-");
                        d.meetingTime = hours[0];
                        d.timeOff = hours[1];
                    }
                    z++;
                    if (z % 3 == 0) {
                        two.append("\n");

                    }

                }
            } catch (Exception e) {
                
            }

        }
        Log.e("27 linjer", two.toString());
        Log.e("dagen: ", d.toString());
        //       d.month =
        return d;
    }

    public ContentValues toEvent() {
        ContentValues event = new ContentValues();
        event.put("title", title);
        event.put("allDay", allday);
        GregorianCalendar sCal = new GregorianCalendar();
        GregorianCalendar eCal = new GregorianCalendar();

        if (title.equals("work")) {
            int meetingHour = Integer.parseInt(meetingTime.split(":")[0]);
            int meetingMinute = Integer.parseInt(meetingTime.split(":")[1]);
            int offHour = Integer.parseInt(timeOff.split(":")[0]);
            int offMinute = Integer.parseInt(timeOff.split(":")[1]);
            sCal.set(2010, month, Integer.parseInt(date), meetingHour, meetingMinute);
            eCal.set(2010, month, Integer.parseInt(date), offHour, offMinute);

        } else {
            sCal.set(2010, month, Integer.parseInt(date));
            eCal.set(2010, month, Integer.parseInt(date));


        }
        event.put("dtstart", sCal.getTimeInMillis());
        event.put("dtend", eCal.getTimeInMillis());

        return event;
    }

    @Override
    public String toString() {
        if (allday == 0) {
            return date + "/" + month + ", mød op: " + meetingTime + ", Gå hjem: " + timeOff + "\n";
        } else {
            return date + "/" + month + "fri \n";
        }

    }

    public void getDetails() {
    }

    public String getRefLink() {
        return refLink;
    }

    public void setRefLink(String refLink) {
        this.refLink = refLink;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getMeetingTime() {
        return meetingTime;
    }

    public void setMeetingTime(String meetingTime) {
        this.meetingTime = meetingTime;
    }

    public String getTimeOff() {
        return timeOff;
    }

    public void setTimeOff(String timeOff) {
        this.timeOff = timeOff;
    }
}
